

#ifndef PBIF_H__
#define PBIF_H__

#include <stdint.h>
#include "pbm.h"


void pbif_decode_packet(pb_msg_t * p_msg, pb_tx_array_handle_t cb);


uint32_t pi_get_firmware_version(void); 

pbd_err_code_t pi_conf_get_dev_information(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_dev_firmware_version(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_dev_project_code(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_dev_mac(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_dev_id(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_mtu(uint8_t *pdata, uint16_t *size); 
pbd_err_code_t pi_conf_get_serial_no(uint8_t* pdata, uint16_t *p_size);
pbd_err_code_t pi_conf_get_logo(uint8_t* pdata, uint16_t *p_size);

pbd_err_code_t pi_conf_get_user_gender(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_user_age(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_user_height(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_user_weight(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_user_skin(uint8_t *pdata, uint16_t *p_size);
pbd_err_code_t pi_conf_get_fatigue_reminder(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_do_not_disturb(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_target_of_day(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_target_of_week(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_proximity(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_notification_category(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_notification_application(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_heartrate_setting(uint8_t *pdata, uint16_t *psize);
pbd_err_code_t pi_conf_get_bloodpressure_setting(uint8_t *pdata, uint16_t *psize);
pbd_err_code_t pi_conf_get_temperature_setting(uint8_t *pdata, uint16_t *psize);
pbd_err_code_t pi_conf_get_sleep_option(uint8_t* pdata, uint16_t *psize); 
pbd_err_code_t pi_conf_get_system_language(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_time_format(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_unit(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_smart_light_on(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_time_of_light_on(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_brightness(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_wearing_habit(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_watchface(uint8_t *pdata, uint16_t *size) ; 
pbd_err_code_t pi_conf_get_system_view_style(uint8_t* pdata, uint16_t *p_size);
pbd_err_code_t pi_conf_get_bt_setting(uint8_t* pdata, uint16_t *size);
pbd_err_code_t pi_conf_get_system_motor_level(uint8_t* pdata, uint16_t *p_size);


pbd_err_code_t pi_conf_write_serial_no(uint8_t *pdata, uint16_t size); 
pbd_err_code_t pi_conf_write_logo(uint8_t *pdata, uint16_t size);

pbd_err_code_t pi_conf_write_user_gender(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_user_age(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_user_height(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_user_weight(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_user_skin(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_fatigue_reminder(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_do_no_disturb(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_target_of_day(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_target_of_week(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_proximity_alert(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_notification_category(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_notification_application(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_heartrate(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_bloodpressure(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_conf_write_temperature(uint8_t* pdata, uint16_t size); 
pbd_err_code_t pi_conf_write_sleep_option(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_language(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_time_format(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_unit(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_smart_light_on(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_time_of_light_on(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_brightness(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_wearing_habit(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_system_watchface(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_conf_write_bt_setting(uint8_t* pdata, uint16_t size );

pbd_err_code_t pi_conf_write_system_motor_vibrate_level(uint8_t* pdata, uint16_t size);
void pi_conf_save_profile(void); 

/// Application 
pbd_err_code_t pi_app_write_alarm_clock(uint32_t idx, uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_app_read_alarm_clock(uint32_t idx, uint8_t *pdata, uint16_t *p_size);

pbd_err_code_t pi_app_read_physic_period(uint8_t* response, uint16_t* maxlen); 
pbd_err_code_t pi_app_write_physic_period(uint8_t* pdata, uint16_t size );


/// Control command part 
pbd_err_code_t pi_ctrl_write_datetime(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_read_datetime(uint8_t *pdata, uint16_t *size);
pbd_err_code_t pi_ctrl_write_day_of_week(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_read_day_of_week(uint8_t *pdata, uint16_t *size);

pbd_err_code_t pi_ctrl_set_battery_notify(uint8_t *pdata, uint16_t size, uint8_t* response, uint16_t *psize);
void pi_ctrl_notify_battery_status(uint8_t is_charging, uint8_t level, uint16_t voltage, pb_tx_array_handle_t cb);
// void pi_ctrl_notify_battery_status(uint8_t is_charging, uint8_t level, uint16_t voltage);

pbd_err_code_t pi_ctrl_get_lcd_time_on(uint8_t* pdata, uint16_t * p_size); 

pbd_err_code_t pi_ctrl_write_hrs_setup_measure(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_write_bloodpressure_setup_measure(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_write_bloodoxygen_setup_measure(uint8_t* pdata, uint16_t size); 
pbd_err_code_t pi_ctrl_write_temperature_setup_measure(uint8_t *pdata, uint16_t size);

#define MEASURE_STATE_SUCCESS       0
#define MEASURE_STATE_NOT_WORN      1
#define MEASURE_STATE_TIMEOUT       2 
#define MEASURE_STATE_SENSOR_NOT_DETECTE    0xFF 
void pi_ctrl_upload_hrs_result_fail(uint8_t reason, pb_tx_array_handle_t cb);
void pi_ctrl_upload_hrs_measure_result(uint8_t state, uint8_t bpm, pb_tx_array_handle_t cb); 
void pi_ctrl_upload_hrs_measure_result_adv(uint8_t average, uint8_t min, uint8_t max, pb_tx_array_handle_t cb);

void pi_ctrl_upload_bloodpressure_result_fail(uint8_t reason, pb_tx_array_handle_t cb);
void pi_ctrl_upload_bloodpressure_measure_result(uint8_t state, uint8_t sbp, uint8_t dbp, pb_tx_array_handle_t cb );

void pi_ctrl_upload_temperature_measure_fail(uint8_t reason, pb_tx_array_handle_t cb);
void pi_ctrl_upload_temperature_measure_result(uint8_t state, int16_t temp, pb_tx_array_handle_t cb); 
void pi_ctrl_upload_temperature_measure_result_adv(uint8_t state, int16_t average, int16_t minimun, int16_t maximum, pb_tx_array_handle_t cb );

void pi_ctrl_upload_spo2_measure_result(uint8_t state, uint8_t average, uint8_t min, uint8_t max, pb_tx_array_handle_t cb);
void pi_ctrl_upload_spo2_result_fail(uint8_t reason, pb_tx_array_handle_t cb);

pbd_err_code_t pi_ctrl_get_add_incoming_call(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_get_remove_incoming_call(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_get_add_missed_call(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_set_message_type(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_set_message_date(uint8_t *date, uint16_t size);
pbd_err_code_t pi_ctrl_set_message_title(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_set_message_content(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ctrl_set_message_content_segment(uint8_t *pdata, uint16_t size); 

pbd_err_code_t pi_ctrl_set_findme(uint8_t * pdata, uint16_t size); 
// void pi_ctrl_send_findme(void); 
void pi_ctrl_send_find_phone(pb_tx_array_handle_t cb);
pbd_err_code_t pi_ctrl_set_camera(uint8_t * pdata, uint8_t size);
pbd_err_code_t pi_ctrl_set_music(uint8_t *pdata, uint8_t size); 
void pi_ctrl_send_take_photo(void); 

pbd_err_code_t pi_ctrl_ems_set(uint8_t* pdata, uint8_t size);
pbd_err_code_t pi_ctrl_heat_set(uint8_t* pdata, uint8_t size );
pbd_err_code_t pi_ctrl_ems_req(uint8_t* pdata, uint8_t size, uint8_t* response, uint16_t *psize);
void pi_ctrl_send_ems_status( pb_tx_array_handle_t cb ); 

pbd_err_code_t pi_ctrl_set_wear_det(uint8_t * pdata, uint16_t size);
void pi_ctrl_send_wear_det(uint8_t condition, uint8_t position);
void pi_ctrl_send_SOS(uint8_t condition); 
void pi_ctrl_send_message_resp(void);

// Memory command part 
pbd_err_code_t pi_mem_write_flash_data( uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_mem_read_flash_data(uint8_t *pdata, uint16_t size, uint8_t *p_read_data, uint16_t * p_byte_of_read);

pbd_err_code_t pi_mem_ext_flash_read_device_id(uint8_t *pdata, uint16_t size, uint8_t *res, uint16_t *res_max_len);
pbd_err_code_t pi_mem_ext_flash_read_jedec_id(uint8_t *pdata, uint16_t size, uint8_t *res, uint16_t *res_max_len);
pbd_err_code_t pi_mem_ext_flash_program_data(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_mem_ext_flash_read_data(uint8_t *pdata, uint16_t size, uint8_t *p_read_data, uint16_t * p_byte_of_read);

pbd_err_code_t pi_mem_accel_read_reg(uint8_t *pdata, uint8_t size, uint8_t * reglistval, uint16_t * reglistlen);
pbd_err_code_t pi_mem_accel_set_fifo_notify(uint8_t *pdata, uint8_t size);
void pi_mem_accel_fifo_notify(uint8_t *pdata, uint8_t size, pb_tx_array_handle_t cb);

pbd_err_code_t pi_mem_tp_data_write_notify(uint8_t* pdata, uint8_t size);
void pi_mem_tp_data_notify(uint8_t* pdata, uint8_t size); 

void pi_mem_hrs_data_notify(uint8_t* pdata, uint8_t size); 

pbd_err_code_t pi_mem_comm_read_reg(uint8_t *pdata, uint16_t size, uint8_t * regval, uint8_t *regvallen);
pb_err_code_t pi_mem_i2c_read_data(uint8_t* pdata, uint8_t size, uint8_t* regval, uint16_t* maxlen); 
pb_err_code_t pi_mem_i2c_write_data( uint8_t* pdata, uint8_t size);


// History Record 
int pi_record_is_empty(uint8_t *req, uint16_t len_of_req); 
pbd_err_code_t pi_record_query(uint8_t *req, uint16_t len_of_req, uint8_t *pdata, uint16_t *p_size);
pbd_err_code_t pi_record_request_upload(uint8_t *req, uint16_t len_of_req, uint8_t *pdata, uint16_t *p_size);
pbd_err_code_t pi_record_reception_ack(uint8_t *req, uint16_t len_of_req, uint8_t *pdata, uint16_t *p_size);
pbd_err_code_t pi_record_stop_session(uint8_t *req, uint16_t len_of_req); 
void pi_record_notify_data(uint32_t address, uint32_t size); 

// DFU 
pbd_err_code_t pi_dfu_init(uint8_t *p_req, uint16_t len, uint8_t *p_res, uint16_t *p_size);
pbd_err_code_t pi_dfu_write(uint8_t *p_req, uint16_t len, uint8_t *p_res, uint16_t *p_size);
pbd_err_code_t pi_dfu_execute(uint8_t *p_req, uint16_t len, uint8_t *p_res, uint16_t *p_size);
pbd_err_code_t pi_dfu_query(uint8_t *p_req, uint16_t len, uint8_t *p_res, uint16_t *p_size);
pbd_err_code_t pi_dfu_reset(uint8_t *p_req, uint16_t len, uint8_t *p_res, uint16_t *p_size);
pbd_err_code_t pi_dfu_query_mtu(uint8_t *p_req, uint16_t len, uint8_t *p_res, uint16_t *p_size);
pbd_err_code_t pi_dfu_version_req(uint8_t *p_req, uint16_t len, uint8_t *p_res, uint16_t *p_size);

void pi_dfu_dfu_init_ok(pb_tx_array_handle_t cb);

// System 
pbd_err_code_t pi_system_set_mode(uint8_t *pdata, uint16_t size); 
pbd_err_code_t pi_system_power_off(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_system_device_reset(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_system_enter_dfu(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_system_enter_tpdfu(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_sytem_enter_ulp(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_system_factory_reset(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_system_clear_history_record(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_system_reset_person_profile(uint8_t *pdata, uint16_t size);


// Factory 
pbd_err_code_t pi_ft_start_session(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ft_stop_session(uint8_t *pdata, uint16_t size);


pbd_err_code_t pi_ft_set_heartrate(uint8_t *pdata, uint16_t size); 

pbd_err_code_t pi_ft_set_vibrate(uint8_t *pdata, uint16_t size);
pbd_err_code_t pi_ft_set_alert(uint8_t *pdata, uint16_t size); 
pbd_err_code_t pi_ft_set_text(uint8_t *pdata, uint16_t size); 
pbd_err_code_t pi_ft_set_lcd_calibrate(uint8_t *pdata, uint16_t size); 

pbd_err_code_t pi_ft_query_selftest_state(uint8_t *pdata, uint16_t size, uint8_t *p_res, uint16_t *p_size);
pbd_err_code_t pi_ft_query_selftest_result(uint8_t* pdata, uint16_t size, uint8_t *p_res, uint16_t *p_size); 

#if 1 // BT module 
// basic 
pbd_err_code_t pi_btm_power_off(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_power_on(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_open_bluetooth(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_close_bluetooth(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_open_uart(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_close_uart(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_reset(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_open_ble(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_close_ble(uint8_t* pdata, uint16_t size);

// query or set 
pbd_err_code_t pi_btm_get_info(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_get_version(uint8_t* pdata, uint16_t size, uint8_t* resp, uint16_t* p_size);
pbd_err_code_t pi_btm_get_bt_mac(uint8_t* pdata, uint16_t size, uint8_t* resp, uint16_t* p_size);
pbd_err_code_t pi_btm_get_vol(uint8_t* pdata, uint16_t size, uint8_t* resp, uint16_t* p_size);
pbd_err_code_t pi_btm_set_hf_vol(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_set_ad_vol(uint8_t* pdata, uint16_t size);

// call 
pbd_err_code_t pi_btm_dialing(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_hangup(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_answer(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_redialing(uint8_t* pdata, uint16_t size);

// phone music 
pbd_err_code_t pi_btm_phone_music_play(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_phone_music_pause(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_phone_music_next(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_phone_music_previous(uint8_t* pdata, uint16_t size);

// pbap 
pbd_err_code_t pi_btm_pbap_start(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_pbap_cancel(uint8_t* pdata, uint16_t size);

// bt 
pbd_err_code_t pi_btm_query_state(uint8_t* pdata, uint16_t size, uint8_t* resp, uint16_t* p_size);
pbd_err_code_t pi_btm_connect_to(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_connect_last(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_bt_disconnect(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_bt_pair(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_bt_reset(uint8_t* pdata, uint16_t size); 
pbd_err_code_t pi_btm_bt_toggle(uint8_t* pdata, uint16_t size);

// mic loop 
pbd_err_code_t pi_btm_open_mic_loop(uint8_t* pdata, uint16_t size);
pbd_err_code_t pi_btm_close_mic_loop(uint8_t* pdata, uint16_t size); 

//ecg
pbd_err_code_t pi_mem_ecg_read_reg(uint8_t *pdata, uint8_t size, uint8_t * reglistval, uint16_t * reglistlen);
pbd_err_code_t pi_mem_ecg_set_task(uint8_t *pdata, uint8_t size);
void pi_mem_ecg_fifo_notify(uint8_t *pdata, uint8_t size, pb_tx_array_handle_t cb);
#endif 

#endif 

